package com.honey.service;

import com.honey.compilation.java.AbstractJavaCompilation;
import com.honey.compilation.java.Clazz;
import com.honey.compilation.java.Interface;
import com.honey.core.compiler.CompilationException;
import com.honey.core.dbmapping.introspect.IntrospectSchema;
import com.honey.core.dbmapping.introspect.IntrospectSchemaColumn;
import com.honey.core.generator.PresentationLayerCompilation;
import com.honey.core.types.FullyQualifiedJavaType;
import com.honey.generator.java.AbstractClassName;

public class ServiceClass extends AbstractClassName {

	private boolean isInterface = false ;
	
	private SampleServicePlugin sampleServicePlugin;

	private PresentationLayerCompilation presentationLayer;

	public ServiceClass(SampleServicePlugin sampleServicePlugin,
			PresentationLayerCompilation presentationLayer,
			boolean isInterface
	) {
		super(sampleServicePlugin);
		this.sampleServicePlugin = sampleServicePlugin;
		this.presentationLayer = presentationLayer;
		this.isInterface = isInterface;
	}
	
	public AbstractJavaCompilation createClass(IntrospectSchema introspectSchema,
			IntrospectSchemaColumn introspectSchemaColumn){
		AbstractJavaCompilation abstractJavaCompilation = null;
		
		try {
			String pkg =sampleServicePlugin.getInterfacePackage();
			String name = introspectSchema.getSchema().getJavaProperty();
			name = super.getClassName(name,sampleServicePlugin.getInterfaceSuffix());
			
			FullyQualifiedJavaType interfaceType = new FullyQualifiedJavaType(pkg+"."+name);
			abstractJavaCompilation = new Interface(interfaceType);
			
			if( !isInterface ){
				pkg =sampleServicePlugin.getImplementsPackage();
				name = introspectSchema.getSchema().getJavaProperty();
				name = super.getClassName(name,sampleServicePlugin.getImplementsSuffix());
				FullyQualifiedJavaType type = new FullyQualifiedJavaType(pkg+"."+name);
				Clazz clazz  = new Clazz(type);
				clazz.addSuperInterface(interfaceType);
				clazz.setAutoAddGetterMethod(true);
				clazz.setAutoAddSetterMethod(true);
				abstractJavaCompilation = clazz;
			}
			if( presentationLayer.requestClass() != null){
				abstractJavaCompilation.addImportedType(presentationLayer.requestClass());
			}
		} catch (CompilationException e) {
			e.printStackTrace();
		}
		return abstractJavaCompilation;
	}
	
}
